/*----------------------------------------------------------------
// Copyright (C) 2011  博览财经资讯服务有限公司
// 版权所有
// 文件名：DBCommSql
// 文件功能描述：
// 
// 创建标识
// 
// 修改标识：
// 修改描述：
//         
// 修改标识：
// 修改描述：
//----------------------------------------------------------------*/

using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data.Common;
using System.Data;
using System.Text.RegularExpressions;

namespace Aij1.Utils
{
    /// <summary>
    /// SqlServer数据库的访问接口实现
    /// </summary>
    public class DBCommSql : IDBComm
    {
        public DBHelper Helper { get; set; }

        /// <summary>
        /// 生成SqlConnection
        /// </summary>
        /// <param name="connectionString"></param>
        /// <returns></returns>
        public DbConnection CreateConnection()
        {
            return new SqlConnection(Helper.ConnStr);
        }

        /// <summary>
        /// 生成SqlCommand
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="connection"></param>
        /// <returns></returns>
        public DbCommand CreateCommand(string sql)
        {
            return new SqlCommand(sql, (SqlConnection)CreateConnection());
        }

        /// <summary>
        /// 生成SqlDataAdapter
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        public DbDataAdapter CreateDataAdapter(DbCommand command)
        {
            return new SqlDataAdapter((SqlCommand)command);
        }

        /// <summary>
        /// 生成SqlParameter
        /// </summary>
        /// <param name="parameterName"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public DbParameter CreateParameter(string parameterName, object value)
        {
            DbParameter param = new SqlParameter(parameterName, CommOp.TestNull(value));
            return param;
        }

        /// <summary>
        /// 生成二进制参数SqlParameter
        /// </summary>
        /// <param name="parameterName"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public DbParameter CreateImageParameter(string parameterName, byte[] value)
        {
            SqlParameter param = new SqlParameter(parameterName, System.Data.SqlDbType.Image);
            param.Value = CommOp.TestNull(value);
            return param;
        }

        public virtual double GetDBSize()
        {
            DataSet ds = Helper.RunProcedureDs("sp_spaceused");
            string r = (string)ds.Tables[0].Rows[0]["database_size"];
            return CommOp.ToDouble(r.Split(' ')[0]);
        }

        public virtual void ShrinkDB()
        {
            string sql = String.Format(@"Backup Log sy13247145681 with no_log
dump transaction sy13247145681 with no_log
USE {0} 
DBCC SHRINKFILE (2)", Helper.GetDBName());
            Helper.ExecNonQuery(sql);
        }
    }
}

